package edu.centenary.assignmenttracker;

import java.util.ArrayList;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.TextView;

public class AssignmentTrackerActivity extends Activity {
    /** Called when the activity is first created. */
	private ArrayList<Assignment> assignments;
	private ListView assignList;
	private Button addButton;
	private AssignAdapter aadapt;
	public static final int AddButtonDialog = 0;
	public static final int DeleteDialog = 1;
	private int where;
	private  MySQLiteHelper helperdb; 
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        assignments = new ArrayList<Assignment>();
        assignList = (ListView)findViewById(R.id.listView1);
        addButton = (Button)findViewById(R.id.button);
        helperdb = new MySQLiteHelper(this);
        
        assignList.setOnItemLongClickListener(new OnItemLongClickListener(){

			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				//
				where = arg2;
				showDialog(DeleteDialog);
				return true;
			}
        	
        });
	
        assignList.setOnItemClickListener(new OnItemClickListener(){

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				
			}

        });

        
        addButton.setOnClickListener(new OnClickListener(){
			
			public void onClick(View v) {
				// Change to talk to db
				SQLiteDatabase db = helperdb.getReadableDatabase();
				ContentValues values = new ContentValues();
				values.put(MySQLiteHelper.TRACKER_TEXT, "myassignment 1");
				values.put(MySQLiteHelper.TRACKER_DATE, "2012-09-26");
				db.insertOrThrow(MySQLiteHelper.TABLE_TRACKER, null, values);
				
				showDialog(AddButtonDialog);
				updateViews();
			}
        });
       
        aadapt = new AssignAdapter(AssignmentTrackerActivity.this,R.layout.row, assignments);
        
        assignList.setAdapter(aadapt);

    }
   
    private void updateViews(){
    	// Fill UP From DAtabase
    	SQLiteDatabase db = helperdb.getReadableDatabase();
    	String[] project = {MySQLiteHelper.TRACKER_DATE,
    		MySQLiteHelper.TRACKER_TEXT};
    Cursor cursor = db.query(MySQLiteHelper.TABLE_TRACKER, project, null, null, null, null, null);
    	cursor.moveToFirst();
    	while (!cursor.isAfterLast()) {
    		Assignment a = new Assignment(cursor.getString(
    			cursor.getColumnIndex(MySQLiteHelper.TRACKER_TEXT)),
    				cursor.getString(
    			cursor.getColumnIndex(MySQLiteHelper.TRACKER_DATE)));
    		assignments.add(a);
    			cursor.moveToNext();
    	}
    	cursor.close();
    	aadapt.notifyDataSetChanged();
    }
    
    protected Dialog onCreateDialog(int id) {
        Context mContext = getApplicationContext();
        Dialog dialog = new Dialog(mContext);
        switch(id) {
        case AddButtonDialog:
        	
    		
    		
    		Builder adb = new AlertDialog.Builder(AssignmentTrackerActivity.this);
			LayoutInflater inflater = (LayoutInflater)AssignmentTrackerActivity.this.getSystemService
		      (Context.LAYOUT_INFLATER_SERVICE);
			View recview = inflater.inflate(R.layout.input, null);
			final EditText taskEdit = (EditText)recview.findViewById(R.id.editTask);
			final EditText date = (EditText) recview.findViewById(R.id.editDate);
			adb.setTitle("New Task");
			adb.setView(recview);
			adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					assignments.add(new Assignment(""+taskEdit.getText(), ""+date.getText(), 1, false, "new"));
 	        	    aadapt.notifyDataSetChanged();
					
				}
			});
			adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					
				}
			});
    		
    		/**builder.setTitle("Add New Task");
    		builder.setMessage("Add new item?");
    		builder.setCancelable(true);
    	    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   assignments.add(new Assignment());
    	        	   aadapt.notifyDataSetChanged();
    	           }
    	       });
    	       **/
    		 dialog = adb.create();
             break;
         case DeleteDialog:

     		AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
     		builder1.setTitle("Delete");
     		builder1.setMessage("Are you sure you want to delete this assignment?");
     		builder1.setCancelable(true);
     		builder1.setNegativeButton("No", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					
					
				}
			});
     	    builder1.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
     	           public void onClick(DialogInterface dialog, int id) {
     	        	  assignments.remove(where);
     	        	  aadapt.notifyDataSetChanged();
     	           }
     	       });
     		 dialog = builder1.create();
              break;
       default:
            dialog = null;
        }
        return dialog;

    }
}